package dao

import javax.inject.Inject

import controllers.RegionData
import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

class BgAnnoDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def insertAll(rows: Seq[BgannoRow]): Future[Unit] = {
    db.run(Bganno ++= rows).map(_ => ())
  }

  def deleteAll: Future[Unit] = db.run(Bganno.delete).map(_ => ())

  def selectAll: Future[Seq[BgannoRow]] = db.run(Bganno.result)

  def selectByGeneId(geneId: String): Future[Option[BgannoRow]] = db.run(Bganno.filter(_.geneid === geneId).result.headOption)

  def selectAllGeneIds: Future[Seq[String]] = db.run(Bganno.map(_.geneid).result)

  def selectAllChrs: Future[Seq[String]] = db.run(Bganno.map(_.chr).distinct.result)

  def selectByRegionData(data: RegionData): Future[Seq[BgannoRow]] = db.run(Bganno.filter(_.chr.toUpperCase === data.chr.toUpperCase)
    .filter(_.start >= data.start).filter(_.end <= data.end).result)

  def selectByGeneIds(geneIds: Seq[String]): Future[Seq[BgannoRow]] = db.run(Bganno.filter(_.geneid.inSetBind(geneIds)).result)


}
